package com.huaxia.controller.deptLevels;

import com.huaxia.pojo.deptLevels.*;
import com.huaxia.service.deptLevels.DeptLevelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author wangli 爱我华夏，爱我中华！
 * @date 2020/08/04 9:26
 */
@RestController
@RequestMapping("/deptLevels")
public class DeptLevelsContrller {

    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private DeptLevelsService deptLevelsService;


    @GetMapping("/getDeptAS")
    public List<DeptAS> getDeptAS(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<DeptAS> deptASs = (List<DeptAS>) servletContext.getAttribute("deptASs");
        if(deptASs==null){
            deptASs = deptLevelsService.getDeptAS();
            servletContext.setAttribute("deptASs",deptASs);
            return deptASs;
        }
        return deptASs;
    }

    @GetMapping("/getDeptBM")
    public List<DeptBM> getDeptBM(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<DeptBM> deptBMs = (List<DeptBM>) servletContext.getAttribute("deptBMs");
        if(deptBMs==null){
            deptBMs = deptLevelsService.getDeptBM();
            servletContext.setAttribute("deptBMs",deptBMs);
            return deptBMs;
        }
        return deptBMs;
    }

    @GetMapping("/getDeptBC")
    public List<DeptBC> getDeptBC(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<DeptBC> deptBCs = (List<DeptBC>) servletContext.getAttribute("deptBCs");
        if(deptBCs==null){
            deptBCs = deptLevelsService.getDeptBC();
            servletContext.setAttribute("deptBCs",deptBCs);
            return deptBCs;
        }
        return deptBCs;
    }

    @GetMapping("/getBranchStandard")
    public List<BranchStandard> getBranchStandard(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchStandard> branchStandards=(List<BranchStandard>)servletContext.getAttribute("branchStandards");
        if(branchStandards==null){
            branchStandards=deptLevelsService.getBranchStandard();
            servletContext.setAttribute("branchStandards",branchStandards);
            return branchStandards;
        }
        return branchStandards;
    }

    @GetMapping("/getSaleDeptStandard")
    public List<SaleDeptStandard> getSaleDeptStandard(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptStandard> saleDeptStandards=(List<SaleDeptStandard>)servletContext.getAttribute("saleDeptStandards");
        if(saleDeptStandards==null){
            saleDeptStandards=deptLevelsService.getSaleDeptStandard();
            servletContext.setAttribute("saleDeptStandards",saleDeptStandards);
            return saleDeptStandards;
        }
        return saleDeptStandards;
    }
}
